<template>
    <div v-if="modelValue && preview" class="image-list">
        <el-image v-if="typeof modelValue == 'string'" :src="modelValue" fit="cover"
            class="w-[100px] h-[100px] rounded border mr-2"></el-image>
        <div v-else class="flex flex-wrap">
            <div class="relative mx-1 mb-2 w-[100px] h-[100px] bg-white rounded-full" v-for="(url, index) in modelValue"
                :key="index">
                <el-icon class="absolute right-[5px] top-[5px] cursor-pointer" style="z-index:10"
                    @click="removeImage(url)">
                    <CircleClose />
                </el-icon>
                <el-image :src="url" fit="cover" class="w-[100px] h-[100px] rounded border mr-2"></el-image>
            </div>
        </div>
    </div>

    <div class="choose-image-btn" @click="open" v-if="preview">
        <el-icon :size="25" class="text-gray-400">
            <Plus />
        </el-icon>
    </div>
    <el-dialog title="选择图片" v-model="dialogVisible" width="80%" top="5vh">
        <el-container class="bg-white rounded" style="height:70vh">
            <el-container>
                <ImageAside ref="ImageAsiderRef" @change="handleAsideChange" />
                <ImageMain ref="ImageMainRef" @choose="hendleChoose" openChoose :limit="limit" />
            </el-container>
        </el-container>
        <template #footer>
            <span>
                <el-button @click="dialogVisible = false" class="mx-2">取消</el-button>
                <el-button type="primary" @click="submit">确定</el-button>
            </span>
        </template>
    </el-dialog>

</template>

<script setup>
import { ref } from "vue";
import ImageAside from "~/components/image/ImageAside.vue";
import ImageMain from "~/components/image/ImageMain.vue";
import { toast } from '~/composables/util.js';

const ImageAsiderRef = ref(null);
const ImageMainRef = ref(null);
const handleOpenCreate = () => ImageAsiderRef.value.handleCreate();
const handleAsideChange = (image_class_id) => {
    ImageMainRef.value.loadData(image_class_id);
}
const handleOpenUpload = () => ImageMainRef.value.openUploadFild();
const dialogVisible = ref(false);

const callBackFunction = ref(null)
const open = (callBack = null) => {
    callBackFunction.value = callBack
    dialogVisible.value = true
}

const props = defineProps({
    modelValue: [String, Array],
    limit: {
        type: Number,
        defalut: 1
    },
    preview: {
        type: Boolean,
        defalut: true
    }
})
const emit = defineEmits(["update:modelValue"])

let urls = [];
const hendleChoose = (e) => {
    urls = e.map(o => o.url)
}
const submit = () => {
    let v = Array.from(new Set([props.modelValue].concat(urls)))
    let value = []
    if (props.limit == 1) {
        value = urls[0]
    } else {
        value = props.preview ? v : [...urls]
        if(value.length > props.limit){
            let limit = props.preview ? (props.limit - props.modelValue.length) : props.limit
            return toast("最多还能选择"+ limit + "张")
        }
    }
    if (value && props.preview) {
        emit("update:modelValue", value)
    }
    if (!props.preview && typeof callBackFunction.value === 'function') {
        callBackFunction.value(value)
    }
    dialogVisible.value = false
}

const removeImage = (url) => {
    emit("update:modelValue", props.modelValue.filter(u => u != url))
}
defineExpose({
    open
})
</script>

<style>
.image-header {
    border-bottom: 1px solid #eee;
    @apply flex items-center;
}

.choose-image-btn {
    @apply w-[100px] h-[100px] rounded border flex justify-center items-center cursor-pointer hover: (bg-gray-100);
}

.image-list {
    margin-top: 0.8rem;
}
</style>